<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>
        blogs
    </title>
    
<link rel="stylesheet" href="/libs/highlight/styles/monokai-sublime.css">

    
<link rel="stylesheet" href="/libs/font-awesome/css/font-awesome.min.css">

    
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<body id="bodyx">
    <div class="hd posts">
    <a href="/index.html"><i class="fa fa-reply replay-btn" aria-hidden="true"></i></a>
    <div class="post-title">
        <p>
            springboot 图片上传fastDfs
        </p>
        <hr>
    </div>
    <div class="post-content">
        <h1 id="spring-boot搭建fastDfs"><a href="#spring-boot搭建fastDfs" class="headerlink" title="spring boot搭建fastDfs"></a>spring boot搭建fastDfs</h1><h2 id="1、上传文件方式一"><a href="#1、上传文件方式一" class="headerlink" title="1、上传文件方式一"></a>1、上传文件方式一</h2><ul>
<li>导入依赖</li>
</ul>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">     &lt;groupId&gt;net.oschina.zcx7878&lt;/groupId&gt;</span><br><span class="line">     &lt;artifactId&gt;fastdfs-client-java&lt;/artifactId&gt;</span><br><span class="line">     &lt;version&gt;1.27.0.0&lt;/version&gt;</span><br><span class="line"> &lt;/dependency&gt;</span><br><span class="line"></span><br><span class="line"> &lt;dependency&gt;</span><br><span class="line">     &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;</span><br><span class="line">     &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;</span><br><span class="line"> &lt;/dependency&gt;</span><br></pre></td></tr></table></figure>



<ul>
<li>创建yml</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">fastdfs:</span><br><span class="line">  connect_timeout_in_seconds: <span class="number">30</span></span><br><span class="line">  network_timeout_in_seconds: <span class="number">60</span></span><br><span class="line">  charset: UTF-<span class="number">8</span></span><br><span class="line">  tracker_servers: 39.105.102.103:22122 # 多个tracker_servers用逗号分割</span><br><span class="line">server:</span><br><span class="line">  port: <span class="number">80</span></span><br></pre></td></tr></table></figure>



<ul>
<li>编写工具类</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"> <span class="meta">@Value</span>(<span class="string">"$&#123;fastdfs.tracker_servers&#125;"</span>)</span><br><span class="line">    <span class="keyword">private</span> String tracker_servers;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"$&#123;fastdfs.connect_timeout_in_seconds&#125;"</span>)</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> connect_timeout;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"$&#123;fastdfs.network_timeout_in_seconds&#125;"</span>)</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> network_timeout;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Value</span>(<span class="string">"$&#123;fastdfs.charset&#125;"</span>)</span><br><span class="line">    <span class="keyword">private</span> String charset;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Map&lt;String,Object&gt; <span class="title">upload</span><span class="params">(MultipartFile multipartFile)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (multipartFile == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"文件不能为空"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 上传至fastDFS, 返回文件id</span></span><br><span class="line">        String fileId = <span class="keyword">this</span>.fdfsUpload(multipartFile);</span><br><span class="line">        System.out.println(fileId);</span><br><span class="line">        <span class="keyword">if</span> (StringUtils.isEmpty(fileId)) &#123;</span><br><span class="line">            System.out.println(<span class="string">"上传失败"</span>);</span><br><span class="line">            <span class="keyword">throw</span>  <span class="keyword">new</span> RuntimeException(<span class="string">"上传失败"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        Map&lt;String, Object&gt; map=<span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">        map.put(<span class="string">"code"</span>,<span class="number">200</span>);</span><br><span class="line">        map.put(<span class="string">"msg"</span>,<span class="string">"上传成功"</span>);</span><br><span class="line">        map.put(<span class="string">"fileId"</span>,fileId);</span><br><span class="line">        <span class="keyword">return</span> map;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 上传至fastDFS</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> multipartFile</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 文件id</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> String <span class="title">fdfsUpload</span><span class="params">(MultipartFile multipartFile)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 1. 初始化fastDFS的环境</span></span><br><span class="line">        initFdfsConfig();</span><br><span class="line">        <span class="comment">// 2. 获取trackerClient服务</span></span><br><span class="line">        TrackerClient trackerClient = <span class="keyword">new</span> TrackerClient();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            TrackerServer trackerServer = trackerClient.getConnection();</span><br><span class="line">            <span class="comment">// 3. 获取storage服务</span></span><br><span class="line">            StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);</span><br><span class="line">            <span class="comment">// 4. 获取storageClient</span></span><br><span class="line">            StorageClient1 storageClient1 = <span class="keyword">new</span> StorageClient1(trackerServer, storeStorage);</span><br><span class="line">            <span class="comment">// 5. 上传文件 (文件字节, 文件扩展名, )</span></span><br><span class="line">            <span class="comment">// 5.1 获取文件扩展名</span></span><br><span class="line">            String originalFilename = multipartFile.getOriginalFilename();</span><br><span class="line">            String extName = originalFilename.substring(originalFilename.lastIndexOf(<span class="string">"."</span>) + <span class="number">1</span>);</span><br><span class="line">            <span class="comment">// 5.2 上传</span></span><br><span class="line">            NameValuePair[] meta_list=<span class="keyword">null</span>;</span><br><span class="line">            String fileId = storageClient1.upload_file1(multipartFile.getBytes(), extName,<span class="keyword">null</span>);</span><br><span class="line">            <span class="keyword">return</span> fileId;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            System.out.println(e);</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 初始化fastDFS的环境</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initFdfsConfig</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            ClientGlobal.initByTrackers(tracker_servers);</span><br><span class="line">            ClientGlobal.setG_connect_timeout(connect_timeout);</span><br><span class="line">            ClientGlobal.setG_network_timeout(network_timeout);</span><br><span class="line">            ClientGlobal.setG_charset(charset);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            System.out.println(e);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<ul>
<li>创建UploadController</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"upload"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UploadController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> UploadService uploadService;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 作上传</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@RequestMapping</span>(<span class="string">"doUpload"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Map&lt;String,Object&gt; <span class="title">doUpload</span><span class="params">(MultipartFile mf)</span></span>&#123;</span><br><span class="line">        System.out.println(mf.getOriginalFilename());</span><br><span class="line">        Map&lt;String, Object&gt; map = uploadService.upload(mf);</span><br><span class="line">        <span class="keyword">return</span> map;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>测试上传文件</li>
</ul>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=<span class="string">"en"</span>&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset=<span class="string">"UTF-8"</span>&gt;</span><br><span class="line">    &lt;title&gt;Title&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &lt;h1&gt;文件上传&lt;/h1&gt;</span><br><span class="line">    &lt;hr&gt;</span><br><span class="line">    &lt;form action=<span class="string">"/upload/doUpload"</span> method=<span class="string">"post"</span> enctype=<span class="string">"multipart/form-data"</span>&gt;</span><br><span class="line">        &lt;input type=<span class="string">"file"</span> name=<span class="string">"mf"</span>&gt;</span><br><span class="line">        &lt;input type=<span class="string">"submit"</span> value=<span class="string">"上传"</span>&gt;</span><br><span class="line">    &lt;/form&gt;</span><br><span class="line"></span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>

<p>注意：file绑定的name与控制层传递的是同一个，不然就会出现上传文件为null</p>
<ul>
<li>遗憾的是我的测试出现了问题，可能是我的远程文件服务器的问题，控制台报的错误是network_timeout</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/20200209180552539.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTk2MzIyMA==,size_16,color_FFFFFF,t_70" alt="上传错误"></p>
<h2 id="2、-上传文件方式二"><a href="#2、-上传文件方式二" class="headerlink" title="2、 上传文件方式二"></a>2、 上传文件方式二</h2><ul>
<li>搭建环境</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">springBootDevTools</span><br><span class="line">Lombok</span><br><span class="line">spring ConfigureProperties</span><br><span class="line">spring-boot-starter-web</span><br></pre></td></tr></table></figure>



<ul>
<li>修改pom</li>
</ul>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- https:<span class="comment">//mvnrepository.com/artifact/com.github.tobato/fastdfs-client --&gt;</span></span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.github.tobato&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;fastdfs-client&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;1.26.7&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>创建配置类UploadProlerites</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ConfigurationProperties</span>(prefix = <span class="string">"upload"</span>)</span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UploadProperties</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String baseUrl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> List&lt;String&gt; allowTypes;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>修改yml</li>
</ul>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">fdfs:</span></span><br><span class="line">  <span class="attr">so-timeout:</span> <span class="number">2500</span>       <span class="comment"># 读取时间</span></span><br><span class="line">  <span class="attr">connect-timeout:</span> <span class="number">600</span>   <span class="comment"># 连接超时时间</span></span><br><span class="line">  <span class="attr">thumb-image:</span>           <span class="comment"># 缩略图</span></span><br><span class="line">    <span class="attr">width:</span> <span class="number">100</span></span><br><span class="line">    <span class="attr">height:</span> <span class="number">100</span></span><br><span class="line">  <span class="attr">tracker-list:</span>          <span class="comment"># tracker服务配置地址列表</span></span><br><span class="line">    <span class="bullet">-</span> <span class="number">116.62</span><span class="number">.44</span><span class="number">.5</span><span class="string">:22122</span></span><br><span class="line"><span class="attr">upload:</span></span><br><span class="line">  <span class="attr">base-url:</span> <span class="string">http://116.62.44.5/</span></span><br><span class="line">  <span class="attr">allow-types:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">image/jpeg</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">image/png</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">image/bmp</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">image/gif</span></span><br></pre></td></tr></table></figure>



<ul>
<li>创建uploadService</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="meta">@EnableConfigurationProperties</span>(UploadProperties<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line"><span class="class"><span class="title">public</span> <span class="title">class</span> <span class="title">UploadService</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> Log log= LogFactory.getLog(UploadService<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> FastFileStorageClient storageClient;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> UploadProperties prop;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">uploadImage</span><span class="params">(MultipartFile file)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 1、校验文件类型</span></span><br><span class="line">        String contentType = file.getContentType();</span><br><span class="line">        <span class="keyword">if</span> (!prop.getAllowTypes().contains(contentType)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"文件类型不支持"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 2、校验文件内容</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            BufferedImage image = ImageIO.read(file.getInputStream());</span><br><span class="line">            <span class="keyword">if</span> (image == <span class="keyword">null</span> || image.getWidth() == <span class="number">0</span> || image.getHeight() == <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"上传文件有问题"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            log.error(<span class="string">"校验文件内容失败....&#123;&#125;"</span>, e);</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"校验文件内容失败"</span>+e.getMessage());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">// 3、上传到FastDFS</span></span><br><span class="line">            <span class="comment">// 3.1、获取扩展名</span></span><br><span class="line">            String extension = StringUtils.substringAfterLast(file.getOriginalFilename(), <span class="string">"."</span>);</span><br><span class="line">            <span class="comment">// 3.2、上传</span></span><br><span class="line">            StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), extension, <span class="keyword">null</span>);</span><br><span class="line">            <span class="comment">// 返回路径</span></span><br><span class="line">            <span class="keyword">return</span> prop.getBaseUrl() + storePath.getFullPath();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            log.error(<span class="string">"【文件上传】上传文件失败！....&#123;&#125;"</span>, e);</span><br><span class="line">            <span class="keyword">throw</span>  <span class="keyword">new</span> RuntimeException(<span class="string">"【文件上传】上传文件失败！"</span>+e.getMessage());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>创建UploadController</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Autwired</span></span><br><span class="line"><span class="keyword">private</span> UploadService uploadService;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> Map&lt;String,object&gt; <span class="title">upload</span><span class="params">(MultipartFile mf)</span></span>&#123;</span><br><span class="line">     Map&lt;String,object&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">    String path =  <span class="keyword">this</span>.uploadService.uploadImage(mf);</span><br><span class="line">    map.put(<span class="string">"path"</span>,path);</span><br><span class="line">    <span class="keyword">return</span> map;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<ul>
<li>测试index.html</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>文件上传<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">"/upload/doUpload"</span> <span class="attr">method</span>=<span class="string">"post"</span> <span class="attr">enctype</span>=<span class="string">"multipart/form-data"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"file"</span> <span class="attr">name</span>=<span class="string">"mf"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"submit"</span> <span class="attr">value</span>=<span class="string">"上传"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>———————-有不足请留言</p>

    </div>
    
        <hr class="fhr">
        <div id="vcomments"></div>
    
</div>
    <div class="footer" id="footer">
    <p>Copyright © 2020 <a class="flink" href="https://hexo.io" target="_blank" rel="noopener">Hexo</a>-<a class="flink" href="https://github.com/sanjinhub/hexo-theme-geek" target="_blank" rel="noopener">Geek</a>.
        <label class="el-switch el-switch-green el-switch-sm" style="vertical-align: sub;">
            <input type="checkbox" name="switch" id="update_style">
            <span class="el-switch-style"></span>
        </label>
<!--         <script type="text/javascript">
        var cnzz_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
        document.write(unescape("%3Cspan id='cnzz_stat_icon_1278548644'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "v1.cnzz.com/stat.php%3Fid%3D1278548644%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));
        </script> -->
    </p>
</div>
<input type="hidden" id="web_style" value="black">
<input type="hidden" id="valine_appid" value="oU6aM6U9xOv4LHEQLo3LLROT-gzGzoHsz">
<input type="hidden" id="valine_appKey" value="TbfimrkuMCSYC9BPMRdOqKga">

<script src="/libs/jquery.min.js"></script>


<script src="/libs/highlight/highlight.pack.js"></script>

<script src='//cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>

<script src="/js/js.js"></script>

<style type="text/css">
.v * {
    color: #698fca;
}

.v .vlist .vcard .vhead .vsys {
    color: #3a3e4a;
}

.v .vlist .vcard .vh .vmeta .vat {
    color: #638fd5;
}

.v .vlist .vcard .vhead .vnick {
    color: #6ba1ff;
}

.v a {
    color: #8696b1;
}

.v .vlist .vcard .vhead .vnick:hover {
    color: #669bfc;
}
</style>
</body>

</html>